package job.manager.icefaces.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *  NotEmpty field validator, checks if the field is empty
 * 
 */
public class NotEmptyValidator implements Validator {

    //validate function that is called.

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if( value instanceof String){
            String s = (String)value;
            if(s.isEmpty()) { //if empty throw exception

                FacesMessage msg = new FacesMessage();
                msg.setDetail("Fältet får inte vara tomt");
                msg.setSummary("Fältet får inte vara tomt");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
        }
    }

}
